import React from 'react'
import PropTypes from 'prop-types'
import {Modal, Form, Input, Select, Avatar, Button, message} from 'antd';
import api from "../../../../api/api";
import axios from "axios";

const {Option} = Select;
const FormItem = Form.Item;

@Form.create()
class resetPassword extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            param: {
                id: "",
                loginPassword: ""
            }
        };
    }

    componentDidMount() {
        console.log("dialog初始化==", this.props);
        this.props.onRef("resetPassword", this)
    }

    handleOk = () => {
        this.props.form.validateFields((error, values) => {
            if (error != null) {
                return;
            }
            let {param} = this.state
            param.loginPassword = values.loginPassword
            let url = api.api_URL + "/admin/logininfo/updateLoginInfo"
            this.setState({param}, () => {
                this.administratorData(url)
            })
        })
    }
    setBaseInfo = () => {
        let data = this.props.data.gridList
        let {param} = this.state
        param.id = data.id;
    };

    //清空form表单数据
    clearData() {
        this.props.form.resetFields();
    }

    //初始化编辑数据
    initEditData = () => {
        this.setBaseInfo()
    }

    //重置密码
    administratorData(url) {
        const that = this;
        axios({
            method: 'POST',
            url: url,
            data: JSON.stringify(that.state.param),
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": "application/json;charset=UTF-8"
            },
        }).then(function (response) {
            if (response.data.code !== 1) {
                message.error({content:"重置密码失败，请重新尝试！"})
                return;
            }
            message.success({content:"重置密码成功！"})
            that.clearData()
            that.handleCancel()
        })
            .catch(function (error) {
                message.error({content:"重置密码失败，请重新尝试！"})

                console.log(error);
            })
    }

    handleCancel = e => {
        console.log(e);
        this.props.handleCancelReset()
    };

    render() {
        const {getFieldDecorator, getFieldError} = this.props.form
        //  密码验证
        const passwordValidator = (rule, value, callback) => {
            const {getFieldValue} = this.props.form;
            if (value && value !== getFieldValue('loginPassword')) {
                callback('两次输入不一致！')
            }
            // 必须总是返回一个 callback，否则 validateFields 无法响应
            callback();
        }
        return (
            <div>
                <Modal
                    title={this.props.data.title}
                    visible={this.props.data.visible}
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                >
                    <Form name="control-ref" autoComplete="off" ref={this.formRef}
                          labelCol={{span: 6}}
                          wrapperCol={{span: 18}}>
                        <FormItem label="密码" name="loginPassword">
                            {getFieldDecorator("loginPassword", {
                                rules: [
                                    {
                                        required: true,
                                        message: "密码不能为空"
                                    }, {
                                        min: 6,
                                        message: '密码不能小于6个字符',
                                    }
                                ]
                            })(
                                <Input.Password/>
                            )}
                        </FormItem>
                        <FormItem label="再次输入密码" name="againLoginPassword">
                            {getFieldDecorator("mobile", {
                                rules: [
                                    {
                                        required: true,
                                        message: "再次输入密码不能为空"
                                    }, {
                                        min: 6,
                                        message: '密码不能小于6个字符',
                                    }, {
                                        validator: passwordValidator
                                    }
                                ]
                            })(
                                <Input.Password/>
                            )}
                        </FormItem>
                    </Form>
                </Modal>
            </div>
        );
    }
}

export default resetPassword